package studentdemo.ui;

import studentdemo.modul.Teach;
import studentdemo.modul.Course;
import studentdemo.modul.Teacher;
import studentdemo.server.TeachService;
import studentdemo.server.CourseService;
import studentdemo.server.TeacherService;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;

public class TeachPanel extends JPanel {
    private TeachService teachService = new TeachService();
    private CourseService courseService = new CourseService();
    private TeacherService teacherService = new TeacherService();
    private JComboBox<Course> courseBox;
    private JComboBox<Teacher> teacherBox;
    private JButton addBtn, deleteBtn, refreshBtn;
    private JTable table;
    private DefaultTableModel tableModel;

    public TeachPanel() {
        setLayout(new BorderLayout());
        // 顶部操作区
        JPanel topPanel = new JPanel();
        courseBox = new JComboBox<>();
        teacherBox = new JComboBox<>();
        addBtn = new JButton("新增授课");
        deleteBtn = new JButton("删除授课");
        refreshBtn = new JButton("刷新");
        topPanel.add(new JLabel("课程:"));
        topPanel.add(courseBox);
        topPanel.add(new JLabel("教师:"));
        topPanel.add(teacherBox);
        topPanel.add(addBtn);
        topPanel.add(deleteBtn);
        topPanel.add(refreshBtn);
        add(topPanel, BorderLayout.NORTH);

        // 表格区
        String[] columns = {"课程编号", "课程名称", "教师编号", "教师姓名"};
        tableModel = new DefaultTableModel(columns, 0) {
            public boolean isCellEditable(int row, int column) { return false; }
        };
        table = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, BorderLayout.CENTER);

        // 事件绑定
        addBtn.addActionListener(e -> addTeachAction());
        deleteBtn.addActionListener(e -> deleteTeachAction());
        refreshBtn.addActionListener(e -> {
            loadCourseBox();
            loadTeacherBox();
            loadTable();
        });
        courseBox.addActionListener(e -> loadTableByCourse());
        teacherBox.addActionListener(e -> loadTableByTeacher());

        loadCourseBox();
        loadTeacherBox();
        loadTable();
    }

    private void loadCourseBox() {
        courseBox.removeAllItems();
        List<Course> courses = courseService.getCoursesByPage(1, 1000);
        for (Course c : courses) {
            courseBox.addItem(c);
        }
    }

    private void loadTeacherBox() {
        teacherBox.removeAllItems();
        List<Teacher> teachers = teacherService.getTeachersByPage(1, 1000);
        for (Teacher t : teachers) {
            teacherBox.addItem(t);
        }
    }

    private void loadTable() {
        tableModel.setRowCount(0);
        List<Teach> teaches = teachService.getAllTeaches();
        for (Teach teach : teaches) {
            Course c = courseService.getCourseById(teach.getTecno());
            Teacher t = teacherService.getTeacherById(teach.getTetno());
            if (c != null && t != null) {
                tableModel.addRow(new Object[]{c.getCno(), c.getCname(), t.getTno(), t.getTname()});
            }
        }
    }

    private void loadTableByCourse() {
        Course c = (Course) courseBox.getSelectedItem();
        if (c == null) return;
        tableModel.setRowCount(0);
        List<Teach> teaches = teachService.getTeachesByCourse(c.getCno());
        for (Teach teach : teaches) {
            Teacher t = teacherService.getTeacherById(teach.getTetno());
            if (t != null) {
                tableModel.addRow(new Object[]{c.getCno(), c.getCname(), t.getTno(), t.getTname()});
            }
        }
    }

    private void loadTableByTeacher() {
        Teacher t = (Teacher) teacherBox.getSelectedItem();
        if (t == null) return;
        tableModel.setRowCount(0);
        List<Teach> teaches = teachService.getTeachesByTeacher(t.getTno());
        for (Teach teach : teaches) {
            Course c = courseService.getCourseById(teach.getTecno());
            if (c != null) {
                tableModel.addRow(new Object[]{c.getCno(), c.getCname(), t.getTno(), t.getTname()});
            }
        }
    }

    private void addTeachAction() {
        Course c = (Course) courseBox.getSelectedItem();
        Teacher t = (Teacher) teacherBox.getSelectedItem();
        if (c == null || t == null) {
            JOptionPane.showMessageDialog(this, "请选择课程和教师");
            return;
        }
        Teach teach = new Teach(c.getCno(), t.getTno());
        boolean success = teachService.addTeach(teach);
        if (success) {
            JOptionPane.showMessageDialog(this, "添加成功");
            loadTable();
        } else {
            JOptionPane.showMessageDialog(this, "添加失败，可能已存在该授课关系");
        }
    }

    private void deleteTeachAction() {
        int row = table.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(this, "请先选中一行");
            return;
        }
        String cno = (String) tableModel.getValueAt(row, 0);
        String tno = (String) tableModel.getValueAt(row, 2);
        int confirm = JOptionPane.showConfirmDialog(this, "确定要删除该授课关系吗？", "确认删除", JOptionPane.YES_NO_OPTION);
        if (confirm == JOptionPane.YES_OPTION) {
            boolean success = teachService.deleteTeach(cno, tno);
            if (success) {
                JOptionPane.showMessageDialog(this, "删除成功");
                loadTable();
            } else {
                JOptionPane.showMessageDialog(this, "删除失败");
            }
        }
    }
} 